package com.synjones.ui.check;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.synjones.entry.SignPerson;
import com.synjones.idcardusb_reader.R;
import com.synjones.ui.FirstScreenRouter;
import com.synjones.ui.SecondScreenRouter;


public class CheckSecondScreen extends FrameLayout implements CheckContract.View {

    public static final int DO_SIGN = 0;
    public static final int DO_PRINT = 1;
    private String invitationCode;

    private SignPerson person;

    private int doWhat = 0;

    private Button checkSign;
    private Button checkCancel;
    private ImageView printSechduleYes;
    private ImageView printSechduleNo;
    private View printSchedule;

    private TextView name;
    private TextView work;
    private TextView hotel;
    private TextView room;
    private TextView sex;


    private FirstScreenRouter router1;
    private SecondScreenRouter router2;


    private CheckSecondPresenter presenter;

    public CheckSecondScreen(@NonNull Context context, String invitationCode,
                             int doWhat, SignPerson person,
                             FirstScreenRouter router1,
                             SecondScreenRouter router2) {
        super(context);
        this.invitationCode = invitationCode;
        this.doWhat = doWhat;
        this.person = person;
        this.router1 = router1;
        this.router2 = router2;
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_check, null);
        setView(view);
        addView(view,new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    public void showBackInfo(String msg, int color) {
        router1.showToast(msg,color);
        router2.showToast(msg,color);
    }

    private void setView(View view){

        checkSign = (Button) view.findViewById(R.id.checkSign);
        checkCancel = (Button) view.findViewById(R.id.checkCancel);
        printSechduleYes = (ImageView) view.findViewById(R.id.printScheduleYes);
        printSechduleNo = (ImageView) view.findViewById(R.id.printScheduleNo);
        printSchedule = view.findViewById(R.id.checkPrintSchedule);
        name = (TextView) view.findViewById(R.id.checkName);
        work = (TextView) view.findViewById(R.id.checkWork);
        sex = (TextView) view.findViewById(R.id.checkSex);
        room = (TextView) view.findViewById(R.id.checkRoom);
        hotel = (TextView) view.findViewById(R.id.checkHotel);

        if (person != null){
            name.setText(person.getName());
            work.setText(person.getWorkPlace());
            sex.setText(person.getSex());
            room.setText(person.getRoomNumber());
            hotel.setText(person.getHotelName());
        }
        if (doWhat == DO_SIGN){
            checkSign.setVisibility(View.VISIBLE);
            printSchedule.setVisibility(View.GONE);
        }else {
            checkSign.setVisibility(View.GONE);
            printSchedule.setVisibility(View.VISIBLE);
        }
        checkSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.Sign(invitationCode,person.getDocumentNumber());
            }
        });
        checkCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                router2.gotoWelcome();
                router1.gotoWelcome();
            }
        });
    }

}
